# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj.py
# @Time     : 2024/4/9 19:22
# @Author   : Robot-Zsj
"""
description:


input
4
8 6 2 8 6
camila 66 70 46 158 80
victoria 94 76 86 189 211
anthony 29 17 83 21 48
emily 53 97 1 19 218

output
victoria
camila
emily
anthony
"""


class Project:
    def __init__(self, name, hot):
        self.name = name
        self.hot = hot


n = int(input())

weights = list(map(int, input().split()))

projects = []

for _ in range(n):
    tmp = input().split()

    name = tmp.pop(0)

    statistics = list(map(int, tmp))

    hot = 0
    for i in range(5):
        hot += statistics[i] * weights[i]

    projects.append(Project(name, hot))

projects.sort(key=lambda x: (-x.hot, x.name.lower()))

for p in projects:
    print(p.name)
